﻿package com.nicoB.tetris {
	import flash.display.MovieClip;	
	
	import com.nicoB.tetris.blocks.ABlock;
	import com.nicoB.tetris.blocks.BlockItem;
	import com.nicoB.tetris.blocks.LBlock;
	import com.nicoB.tetris.blocks.LineBlock;
	import com.nicoB.tetris.blocks.SBlock;
	import com.nicoB.tetris.blocks.SquareBlock;
	import com.nicoB.tetris.blocks.TBlock;
	import com.nicoB.tetris.constants.Blocks;
	
	import flash.display.Sprite;
	import flash.text.TextField;	

	/**
	 * @author n.bush
	 * @project tetris
	 * @date 5 nov. 2008
	 * @desc 
	 */
	public class ScorePannel extends Sprite {
		
		public var txtLevel : TextField;
		public var txtScore : TextField;
		public var txtLines : TextField;

		private var _blockWidth : uint;

		private var _nextBlock : MovieClip;

		public function ScorePannel() {
		}
		
		public function set blockWidth(p : uint):void{
			_blockWidth = p;
		}
		
		public function set level(p : uint):void{
			txtLevel.text = p.toString();
		}
		
		
		public function set score(p : uint):void{
			txtScore.text = p.toString();
		}
		
		public function set lines(p : uint):void{
			txtLines.text = p.toString();
		}

		public function showNextBlock(type : String) : void {
			
			if (_nextBlock != null){
				removeChild(_nextBlock);
				_nextBlock = null;
			}
			
			//on recupere le type de block
			var blockType : ABlock;
			switch (type){
				case Blocks.L:
					blockType = new LBlock();
					break;
				case Blocks.S:
					blockType = new SBlock();
					break;
				case Blocks.T:
					blockType = new TBlock();
					break;
				case Blocks.LINE:
					blockType = new LineBlock();
					break;
				case Blocks.SQUARE:
					blockType = new SquareBlock();
					break;
			}
			
			_drawBlock(blockType);
		}
		
		
		private function _drawBlock(blockType : ABlock):void{
			
			var cells : Array = blockType.cells;
			
			_nextBlock = new MovieClip();
			_nextBlock.x = 3 + 110 / 2 - blockType.width * _blockWidth / 2;
			_nextBlock.y = 65;
			addChild(_nextBlock);
			
			
			for (var i : uint = 0; i < cells.length; i++) {
				
				for (var j : uint = 0; j < cells[i].length; j++) {
					if (cells[i][j] != 0){
						_drawCell(j, i, cells[i][j]);
					}
					
				}
			}
			
		}
		
		
		
		
		private function _drawCell(line : uint, col : uint, color : uint) : BlockItem {
			
			var block : BlockItem = new BlockItem();
			block.x = line * _blockWidth;
			block.y = col * _blockWidth;
			block.color = color;
			_nextBlock.addChild(block);
			
			return block;
		}
	}
}
